<?php
#require_once('TVB/pms/pdes/common/util/ArrayDiff.class.php');

class P_CastGetAction extends BaseAction 
{
	public $id;
	public $type;
	public $step;
	public $act;
	public $unpublish;

	public $episode_id;
	public $programme_id;

	public $ut;
	public $from;
	public $error_code;

	public function execute(){
		if(!is_null($this->error_code))
		{
			$this->addError('Cast.Edit', array($this->error_code, 'Cast.Edit'));	
		}

		try
		{
			# add publish log
			$this->setData("publishLog", publish_log::getPublishLog('cast', $this->id));
            $this->setData('special_operation_premium', TRUE);

			if(!empty($this->unpublish)) {Model::$dbo['r'] = CommonUtils::getDbConn(0,'preview');}

			//highlight
			$this->folder_highlight('cast', $this->id);

			//cast detail 
			$cast = new p_cast();
			$cast_detail = $cast->getDetails($this->id);
			$cast_detail->role_name = $cast_detail->system_label;
			$this->setData("cast", $cast_detail);
			//programme
			$programme = new p_programme();
			$programme_detail = $programme->getDetails($cast_detail->programme_id);
			//handle multiple language
			$programme_name = explode("\n\t",$programme_detail->mix_programme_name);
			$programme_detail->zh_programme_name = $programme_name[0];
			$programme_detail->en_programme_name = $programme_name[1];
			$this->setData("programme", $programme_detail);

			//cast role list
			$cast_role = new cast_role();
			$cast_role_list = $cast_role->get();
			$this->setData('cast_role_list', $cast_role_list);

			//tag list
			$tag_list = array();
			if (!empty($cast_detail->tags))
			{
				$tag = new p_tag();
				$tag_all = $tag->getTagList($cast_detail->tags);
				foreach ($tag_all as $tag)
				{
					$tag_list[$tag->typeid][] = array("id"=>$tag->id,"system_label"=>$tag->system_label);
				}
			}

			//alow tag
			$allow_tag = CommonUtils::get_allow_tags("cast");
			$tag_type = new tag_type();
			$allow_tag_list = $tag_type->findByInSql($allow_tag);
			$tag_arr = array();
			foreach ($allow_tag_list as $tag)
			{
				$tag_arr[$tag->id] = array("id"=>$tag->id,"name"=>$tag->system_label,'restrict'=>$tag->restricted);
				if (empty($cast_detail->tags))
				{
					$tag_list[$tag->id] = array();
				}
			}
			$this->setData("allow_tag", $tag_arr);
			$this->setData("tag_list", $tag_list);

			//folder list
			$folder_list = array();
			$folders = array();
			if (!empty($cast_detail->folders))
			{
				$folder = new p_folder();
				$all_folder = $folder->findByInSql($cast_detail->folders);

				$is_published = CommonUtils::is_published('cast',$this->id);
				$image_folders = array();

				foreach ($all_folder as $single_folder)
				{
					$item_list = $folder->get_folder_items($single_folder['id']);
					if (!empty($item_list))
					{
						foreach ($item_list as &$item)
						{
							$item['title']  = 'ID:'.$item['id'].'<br />'.htmlentities($item['title'], ENT_QUOTES, 'UTF-8');
							$item['status'] = '';
							$item['flag']   = null;
						}
					}
					$folders = array(
						'fid'	=> $single_folder['id'],
						'fspec'	=> $single_folder['folder_type_id'],
						'total'	=> $single_folder['total'],
						'ftype'	=> $single_folder['folder_item_type_id'],
						'path'	=> $single_folder['path'],
						'fname'	=> htmlentities($single_folder['system_label'], ENT_QUOTES, 'UTF-8'),
						'items'	=> $item_list);

					if ($single_folder['folder_item_type_id'] == AC::FOLDER_TYPE_IMAGE)
					{
						$image_folders[] = $folders;
					}
				}
				$folder_list = array('pid'=>$cast_detail->id,'ptype'=>'cast','folders'=>$image_folders);
			}
			$this->setData("folder_list", $folder_list);

			/**
			//folder list
			$folder_list = array();
			$folders = array();
			if (!empty($cast_detail->folders))
			{
				$folder = new folder();
				$all_folder = $folder->findByInSql($cast_detail->folders);
				$diff = $this->_data['diff_folder'];

				$is_published = CommonUtils::is_published('cast',$this->id);
				$video_folders = $image_folders = array();

				foreach ($all_folder as $single_folder)
				{
					if ($this->act == 'view' && !$this->unpublish)
					{
						$folders = CommonUtils::getDiffFolderItem($single_folder,$this->act,$diff, $this->unpublish, $is_published);
					}
					else
					{
						$item_list = $folder->get_folder_items($single_folder['id']);
						if (!empty($item_list))
						{
							foreach ($item_list as &$item)
							{
								$item['title']  = 'ID:'.$item['id'].'<br />'.htmlentities($item['title'], ENT_QUOTES, 'UTF-8');
								$item['status'] = '';
								$item['flag']   = null;
							}
						}
						$folders = array(
							'fid'	=> $single_folder['id'],
							'fspec'	=> $single_folder['folder_type_id'],
							'total'	=> $single_folder['total'],
							'ftype'	=> $single_folder['folder_item_type_id'],
							'fname'	=> htmlentities($single_folder['system_label'], ENT_QUOTES, 'UTF-8'),
							'items'	=> $item_list);
					}

					if ($single_folder['folder_item_type_id'] == AC::FOLDER_TYPE_VIDEO)
					{
						$video_folders[] = $folders;
					}
					elseif ($single_folder['folder_item_type_id'] == AC::FOLDER_TYPE_IMAGE)
					{
						$image_folders[] = $folders;
					}
				}
				$folder_list = array('pid'=>$cast_detail->id,'ptype'=>'cast','folders'=>$image_folders);
			}
			$this->setData("folder_list", $folder_list);
			 */

			$this->setData("id", $this->id);
			$this->setData("type", 'cast');
			$this->setData("step", $this->step);
			$this->setData("act", $this->act);
			$this->setData("episode_id", $this->episode_id);
			$this->setData("programme_id", $this->programme_id);
			$this->setData("from", $this->from);
			$this->setData('unpublish', $this->unpublish);

			return 'succ';
		}
		catch(Exception $e)
		{
			error_log("cast get: {$this->id}" . var_export($e,1));

			$this->emptyData();
			return 'system-error';
		}
	}    

	/**
	 * highlight init handle
	 */
	public function folder_highlight($type, $id)
	{
		// modified by vanni at 2010-04-06 for bug:3686
		return true;

		$db_local  =  CommonUtils::getDbConn()->getDbHandle();
		$db_remote =  CommonUtils::getDbConn(0, 'preview')->getDbHandle();

		$proof = new ProofreadSend();
		$local_arr = $proof->getProofreadData($db_local, $type, $id); 
		$remote_arr = $proof->getProofreadData($db_remote, $type, $id); 
		$diff_folder = ArrayDiff::multi_diff($local_arr, $remote_arr);
		$this->setData("diff_folder", $diff_folder);
	}
}
?>
